package com.xiaozhi.utils;

import com.xiaozhi.VO.RegisterVO;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Iterator;


public class ChangeVO {

    /**
     * 将用户相关的信息生成相应的对象
     * @param registerVO 信息来源
     * @param tClass Class对象
     * @return Class对象对应的实例对象
     */
    public static  <T> T userVO(RegisterVO registerVO, Class<T> tClass) throws IllegalAccessException, InstantiationException {
        T t = tClass.newInstance();

        Field[] fields = tClass.getDeclaredFields();
        Iterator<Field> it = Arrays.stream(fields).iterator();
        while (it.hasNext()){
            Field next = it.next();
            next.setAccessible(true);
            switch (next.getName()){
                case "serialVersionUID":break;
                case "account":next.set(t,registerVO.getAccount());break;
                case "password":next.set(t,registerVO.getPassword());break;
                case "email":next.set(t,registerVO.getEmail());break;
                case "name":next.set(t,registerVO.getName());break;
                case "gender":next.set(t,registerVO.getGender());break;
                case "age":next.set(t,registerVO.getAge());break;
                case "score":next.set(t,100);break;
                case "duration":next.set(t,"0");break;
                default:next.set(t,null);
            }
            //获取到该对象的所用字段
            //System.out.println(next.getName());
        }
        return t;
    }


}

